home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!news
- From: jhewett@ix.netcom.com (Jerry Hewett)
- Newsgroups: comp.lang.c++
- Subject: Re: Did I Miss Something?
- Date: Thu, 04 Apr 96 18:06:20 GMT
- Organization: Netcom
- Message-ID: <N.040496.100620.06@ix.netcom.com>
- References: <N.040396.105136.51@ix.netcom.com>
- <4k01o7$sl2@grimsel.zurich.ibm.com>
- NNTP-Posting-Host: tem-ca1-07.ix.netcom.com
- X-NETCOM-Date: Thu Apr 04 12:05:48 PM CST 1996
- X-Newsreader: Quarterdeck Message Center [2.00]
-
- Keith Whittingham <wgk@zurich.ibm.com> writes:
-
- > There is no memory allocation for the string: your assumption is wrong. I
- > think the code you're are being given is not a very good example! The
- > constructer would be better written:
- >
- > box::box(char *input_line)
- > {
- > length = 8;
- > width = 8;
- > line_of_text = strdup(input_line);
- > }
-
- So if my theory that the act of calling a constructor allocates the necessary
- memory (like strdup automagically calls malloc to create space in the heap) is
- in error, then the original example (line_of_text = input_line) indeed does not
- carve out a chuck of RAM big enough to hold "small box"? So even though this
- works when I compile it, in actuality it is undefined behavior?
-
- Since I posted this question yesterday I've been looking through all of the
- reference books I have on hand, and discovered the following information on
- page 126 of Herbert Schildt's _C++ Nuts & Bolts_:
-
- ----<snip>----
-
- "As you know, C++ allows string constants enclosed between double
- quotes to be used in a program. When the compiler encounters such
- a string, it stores it in the program's string table and generates
- a pointer to the string. For this reason, the following program is
- correct and prints 'one two three' on the screen:"
-
- #include <iostream.h>
-
- main()
- {
- char *p;
-
- p = "one two three";
-
- cout << p;
-
- return 0;
- }
-
- ----<snip>----
-
- Well, this is all certainly news to me! :-)
-
- (please bear with me on this -- I'm really trying to make sure I understand
- what's going on here so that I can prevent "Stupid Programmer Behavior" in my
- own code)
-
- What Schildt is apparently saying is that even though "char *p" is NOT being
- declared as "const char *p", the compiler will treat it as a constant by adding
- it to a string table in memory and returning a pointer to it? Therefore, the
- original snippet of sample code...
-
- > class box {
- > int length;
- > int width;
- > char *line_of_text;
- > public:
- > box(char *input_line);
- > void set(int new_length, int new_width);
- > int get_area(void);
- > };
- > box::box(char *input_line)
- > {
- > length = 8;
- > width = 8;
- > line_of_text = input_line;
- > }
- >
- > main()
- > {
- > box small("small box ");
- >
- > }
-
- ..is correct according to the rules of C++ (even though it possibly could have
- been defined more clearly to prevent folks like me from misunderstanding what's
- going on ;-)?
-
- > Of course the C++ biggots will insist on using new and delete instead of
- > strdup and free but that's all a matter of style.
-
- I agree with everything in your revised version of the code, and probably would
- have done something similar myself, except that I probably would have used
- "new" and "delete" to help me along in the learning process. But hashing this
- kind of strange (to me, at least :-) behavior out with other programmers has
- always helped me grasp and hold onto new concepts and ideas. At least I
- *think* I have a better understanding of what's going on now...
-
- > In general C++ itself does no more for you than C, no automatic memory
- > allocation, no garbage collection etc. All in all that should lead to
- > faster code but you do have to be thorough when writing the stuff.
-
- Especially when an example like the one I posted confuses the heck out of a
- newcomer to C++ like me! :-)
-
- Jerry H.
-
-
-